package BanyuanClub.Work.Day0301;

public class MyLinkedList {
    private Node first;
    private int size;
    //增头
    public void addFirst(Object obj){
        Node temp = new Node();
        temp.nist = first;
        temp.date = obj;
        first = temp;
        size++;
    }
    //增尾
    public void addList(Object obj){
        Node temp = new Node();
        temp.date = obj;
        if (first == null){
            first = temp;
        }
        Node ti = first;
        if (ti.nist != null){
            ti = ti.nist;
        }
        ti.nist = temp;
        size++;
    }
    //掐头
    public void removeFirst(){
        if (first == null){
            throw new IllegalStateException();
        }
        Node temp = first.nist;
        first.nist = null;
        first.date = null;
        first = temp;
    }
    //去尾
    public void removeList(){
        if (first == null){
            throw new IllegalStateException();
        }
        if (first.nist == null){
            first.date = null;
            return;
        }
        Node temp = first;
        while (temp.nist.nist != null){
            temp = temp.nist;
        }
        temp.nist.date = null;
        temp.nist = null;

    }
    //查询
    public Object get(int index){
        if (index < 0 || index >= size){
            return null;
        }
        if (size == 0){
            return null;
        }
        Node temp = first;
        for (int i = 0; i < index; i++) {
            temp = temp.nist;
        }
        return temp.date;
    }
}

class Node{
    public Object date;
    public Node nist;
}